/**
 * JAVACC DEMO 1.0
 */
package com.apache.portal.weixin.service;

import java.util.List;

import com.apache.cache.util.Validator;
import com.apache.portal.weixin.core.common.BaseService;
import com.apache.portal.weixin.entity.InfoVo;
import com.apache.portal.weixin.entity.send.WxSendMsg;
import com.apache.portal.weixin.entity.send.WxSendNewsMsg;
import com.apache.uct.common.ToolsUtil;

/**
 * description:  
 * @author dyhou 创建时间：2017-3-2  
 */
public abstract class AbstractService implements BaseService {

	protected WxSendMsg getInfos(List<InfoVo> list, WxSendMsg sendMsg) throws Exception {
		if (Validator.isEmpty(list)) {
			return getWxSendMsg(sendMsg, "请您稍后关注，版本更新中...");
		} else {
			WxSendNewsMsg news = new WxSendNewsMsg(sendMsg);
			for (int i = 0; i < list.size(); i++) {
				InfoVo vo = list.get(i);
				news.addItem(vo.getTitle(), vo.getDescription(), vo.getInfoPic(), vo.getUrl());
			}
			return news;
		}
	}

	protected WxSendMsg getWxSendMsg(WxSendMsg sendMsg, String text) {
		WxSendNewsMsg news = new WxSendNewsMsg(sendMsg);
		news.addItem("信息提示", text, "", "");
		return news;
	}

	/**
	 * 消息  带URL
	 */
	protected InfoVo getInfoVoFromProp(String key) throws Exception {
		String text = ToolsUtil.getInstance().getValueByKey(key);
		if (("".equals(text)) || (text == null))
			text = "请您稍后关注，版本更新中...";
		InfoVo news = new InfoVo();
		news.setTitle(Validator.getDefaultStr(new String(ToolsUtil.getInstance().getValueByKey("title_" + key)
				.getBytes("iso8859-1"), "utf-8"), ""));
		news.setDescription(new String(text.getBytes("iso8859-1"), "utf-8"));
		String pic = Validator.getDefaultStr(ToolsUtil.getInstance().getValueByKey("pic_" + key), "");
		if (!Validator.isEmpty(pic)) {
			if (!pic.startsWith("http")) {
				pic = ToolsUtil.getInstance().getValueByKey("web_http") + pic;
			}
		}
		news.setInfoPic(pic);
		news.setUrl(Validator.getDefaultStr(ToolsUtil.getInstance().getValueByKey("url_" + key), ""));
		return news;
	}

}
